#include "QCefExtWidget.h"

QCefExtWidget::QCefExtWidget(QWidget * parent, Qt::WindowFlags flags)
#ifdef QT_CEF_OSR
    : QCefOsrWidget(parent, flags)
#else
    : QCefNcwWidget(parent, flags)
#endif
{
    QCEF_DEBUG();
}

QCefExtWidget::QCefExtWidget(const QString & strUrl, QWidget * parent, Qt::WindowFlags flags)
#ifdef QT_CEF_OSR
    : QCefOsrWidget(strUrl, parent, flags)
#else
    : QCefNcwWidget(strUrl, parent, flags)
#endif
{
    QCEF_DEBUG();

    //m_strFirstUrl = strUrl;
    //createContainer();
    //createClient();
}

QCefExtWidget::~QCefExtWidget()
{
    QCEF_DEBUG();

    if (m_delegate != nullptr)
    {
        m_delegate->finiDelegate();

        //delete m_delegate;
        m_delegate.release();
        m_delegate = nullptr;
    }
}


int QCefExtWidget::createClient()
{
    if (m_delegate == nullptr)
    {
        m_delegate = this;  //记得调用m_delegate.release()取消关联，否则会出现对象重复删除。

    #ifdef QT_CEF_OSR
        m_client = new QCefOsrClient(QCefBrowserApp::instance());
    #else
        m_client = new QCefNcwClient(QCefBrowserApp::instance());
    #endif


        m_delegate->setOperator(this);
        m_client->setDelegate(m_delegate);
    }

    return 0;
}

void QCefExtWidget::closeEvent(QCloseEvent *event)
{
    QCEF_DEBUG();

    if (m_delegate != nullptr)
    {
        m_delegate->finiDelegate();

        //delete m_delegate;
        m_delegate.release();
        m_delegate = nullptr;
    }

    //
    __super::closeEvent(event);
}
